﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using Common;
using Basic.Model.Entity;
using Basic.Model.EntityManager;

namespace Basic.Business
{
    public class RoleAdmin
    {
        #region 验证一个用户是否有该权限
        /// <summary>
        /// 验证一个用户是否有该权限
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="roleNames"></param>
        /// <returns></returns>
        public static bool CheckRole(string roleName, string roleNames)
        {
            if (String.IsNullOrEmpty(roleName))
                return false;
            string[] roleKeys=roleName.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < roleKeys.Length; i++)
            {
                if (roleNames.IndexOf(roleKeys[i] + ",") > 0)
                    return true;
            }
            return false;
        }
        public static bool CheckRole(string roleName, Basic.VO.Admin admin)
        {
            if (admin == null)
                return false;
            return CheckRole(roleName, admin.adminGroup.roleNames);
        }
        #endregion

        #region 添加一个权限
        /// <summary>
        /// 添加一个权限
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="roleGroupId"></param>
        public static void AddRole(string roleName, int roleGroupId)
        { 
            ISession session = null;
            ITransaction trans = null;
            Role role = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                
                if (RoleManager.GetByCondition(session, -1, roleName, false) != null)
                    throw new Exception("当前系统已存在这个权限，请换一个权限名。");
                role = new Role();
                role.roleName = roleName;
                RoleGroup roleGroup = RoleGroupManager.Get(session, roleGroupId);
                if (roleGroup == null)
                    throw new Exception("当前权限所选择权限组不存在或者已经被删除。");
                role.roleGroup = roleGroup;
                RoleManager.Add(session, role);

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        /// <summary>
        /// 添加一个权限
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="roleGroupName"></param>
        public static void AddRole(string roleName, string roleGroupName)
        {
            ISession session = null;
            ITransaction trans = null;
            Role role = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                if (RoleManager.GetByCondition(session, -1, roleName, false) != null)
                    throw new Exception("当前系统已存在这个权限，请换一个权限名。");
                role = new Role();
                role.roleName = roleName;
                RoleGroup roleGroup = RoleGroupManager.GetByCondition(session, -1, roleGroupName, false);
                if (roleGroup == null)
                    throw new Exception("当前权限所选择权限组不存在或者已经被删除。");
                role.roleGroup = roleGroup;
                RoleManager.Add(session, role);

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion
    }
}
